package com.gohi.main.util;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;



import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
import net.sf.json.util.PropertyFilter;
/**
 * JSON 输出对象肖得明
 * @author Administrator
 *
 */
public class AppJsonUtils { 
  public static	Logger logger = Logger.getLogger(AppJsonUtils.class);
	//屏蔽对象里面的SET对象
	public static JsonConfig config = new JsonConfig();
	static{
		config.setIgnoreDefaultExcludes(false);
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		//config.setExcludes(new String[]{"handler","hibernateLazyInitializer"});     
		config.setJsonPropertyFilter(new PropertyFilter() { 
			    public boolean apply(Object source, String name, 
			      Object value) { 
			     if(value!=null){ 
			      String typeName = value.getClass().getName(); 
			      if (typeName.equals("org.hibernate.collection.PersistentSet") 
			        ) { 
			       return true; 
			      } 
			     } 
			     return false; 
			    } 
			   }); 
		config.registerJsonValueProcessor(java.sql.Timestamp.class, new DateJsonValueProcessor());  
	}
	/**
	 * 输出单个JSON数据
	 * @param o
	 * @param response
	 * @throws IOException
	 */
	public static void ptinrJson(Object o ,HttpServletRequest request, HttpServletResponse response){
		String callbackfun = request.getParameter("callbackfun");//JSON跨域
		String result =JSONSerializer.toJSON(o,config).toString();
		if(callbackfun!=null && !callbackfun.equals("")){
			result=callbackfun+"("+result+")";
		}
		response.setContentType("application/json;charset=GBK");
		//System.err.println(result);
		try {
			response.getWriter().print(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			logger.error("输入出JSON流错误！");
			e.printStackTrace();
		}
	}
	
	public static void ptinrJson(List<Object> o ,HttpServletRequest request, HttpServletResponse response) throws IOException{
		String callbackfun = request.getParameter("callbackfun");//JSON跨域
		String result =JSONSerializer.toJSON(o,config).toString();
		if(callbackfun!=null && !callbackfun.equals("")){
			result=callbackfun+"("+result+")";
		}
		response.setContentType("application/json;charset=GBK");
		//System.err.println(result);
		response.getWriter().print(result);
	}
	
	public static String getJson(List<Object> o) {
		String result =JSONSerializer.toJSON(o,config).toString();
		return result;
	}
	//输出的时候 自定义屏蔽的参数
	public static void ptinrJson(List<Object> o ,HttpServletRequest request, HttpServletResponse response,String[] pars) throws IOException{
		JsonConfig con = new JsonConfig();
		con.setIgnoreDefaultExcludes(false);
		con.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		con.setExcludes(pars);     
		con.setJsonPropertyFilter(new PropertyFilter() { 
			    public boolean apply(Object source, String name, 
			      Object value) { 
			     if(value!=null){ 
			      String typeName = value.getClass().getName(); 
			      if (typeName.equals("org.hibernate.collection.PersistentSet") 
			        ) { 
			       return true; 
			      } 
			     } 
			     return false; 
			    } 
			   }); 
		con.registerJsonValueProcessor(java.sql.Timestamp.class, new DateJsonValueProcessor());  
		String callbackfun = request.getParameter("callbackfun");//JSON跨域
		String result =JSONSerializer.toJSON(o,con).toString();
		if(callbackfun!=null && !callbackfun.equals("")){
			result=callbackfun+"("+result+")";
		}
		response.setContentType("application/json;charset=GBK");
		//System.err.println(result);
		response.getWriter().print(result);
	}
	//输出的时候 自定义屏蔽的参数
	public static void ptinrJson(Object o ,HttpServletRequest request, HttpServletResponse response,String[] pars) throws IOException{
		JsonConfig con = new JsonConfig();
		con.setIgnoreDefaultExcludes(false);
		con.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		con.setExcludes(pars);     
		con.setJsonPropertyFilter(new PropertyFilter() { 
			    public boolean apply(Object source, String name, 
			      Object value) { 
			     if(value!=null){ 
			      String typeName = value.getClass().getName(); 
			      //屏蔽SET对象
			      if (typeName.equals("org.hibernate.collection.PersistentSet") 
			        ) { 
			       return true; 
			      } 
			     } 
			     return false; 
			    } 
			   }); 
		con.registerJsonValueProcessor(java.sql.Timestamp.class, new DateJsonValueProcessor());  
		String callbackfun = request.getParameter("callbackfun");//JSON跨域
		String result =JSONSerializer.toJSON(o,con).toString();
		if(callbackfun!=null && !callbackfun.equals("")){
			result=callbackfun+"("+result+")";
		}
		response.setContentType("application/json;charset=GBK");
		//System.err.println(result);
		response.getWriter().print(result);
	}
}
